package com.kk.miaosha.event;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kk.miaosha.entity.ItemKillSuccess;
import com.kk.miaosha.mapper.ItemKillSuccessMapper;
import com.kk.miaosha.utils.MqConstantUtil;
import com.kk.miaosha.utils.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * @author : K k
 * @date : 21:07 2020/9/26
 */
@Component
@Slf4j
@Transactional
public class EventConsumer implements MqConstantUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ItemKillSuccessMapper iksMapper;
    /*@RabbitListener(queues = {QUEUE_KILL})
    public void handleCommentMessage(Event event){
        if(event==null){
            log.error("消息格式错误");
            return;
        }
        //构造订单
        SnowflakeGenerator snowflakeGenerator=new SnowflakeGenerator();
        Long id = snowflakeGenerator.next();
        //查询该雪花id是否使用过
        while (randomCodeService.findSnowId(id)){
            //若存在则重新创建
            id=snowflakeGenerator.next();
        }
        //创建新的randomCode
        randomCodeService.save(new RandomCode().setCode(Convert.toStr(id)));
        ItemKillSuccess order=new ItemKillSuccess();
        order.setId(Convert.toStr(id)).setItemId(event.getItemId())
                .setKillId(event.getKillId()).setUserId(event.getUserId());
        //下入数据库
        boolean save = iksService.save(order);
        //TODO 发送短信
        if (save){
            log.info("创建订单成功！");
        }else {
            log.error("创建订单失败！");
        }

    }*/

    //监听从延时队列过期发送到死信队列的消息
    @RabbitListener(queues = {MqConstantUtil.DLX_QUEUE})
    public void receive(Map<String,Object> map){
        System.out.println("------接收到过期的消息-----："+new Date());
        System.out.println("------接收到的消息内容-----：\n"+map);
        //删除redis中的账单并回滚

        //1.获取账单唯一标识uuid
        String uuid = (String) map.get("uuid");
        Integer killId = (Integer) map.get("killId");
        Integer itemId = (Integer) map.get("itemId");
        Integer  userId = (Integer) map.get("userId");
        //将订单中的用户对应订单删除
        redisTemplate.opsForHash().delete(RedisKeyUtil.getKillSuccessKey(userId), uuid);

        //查看是否生成订单，若无则将uuid添加回库存 交给死信队列来监听过期
        ItemKillSuccess iks = iksMapper.selectOne(new QueryWrapper<ItemKillSuccess>().eq("user_id", userId).eq("kill_id", killId));
        if (iks!=null){
            System.out.println("------下单成功-------");
        }else {
            System.out.println("------回滚成功-------");
            redisTemplate.opsForList().rightPush(RedisKeyUtil.getKillStockKey(itemId,killId),uuid);
        }
    }

}
